<?php

namespace Api\Controller;


class RewardController extends PublicController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 打赏接口
     * type  comment/video
     * id    comment_id/video_id
     * money integer
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function doReward()
    {
        $this->checkLogin();
        $type = I('type', '', 'trim');
        $money = I('money', 0, 'intval');
        $target_id = I('id', 0, 'intval');

        $res = event('reward')->doReward(UID, $target_id, $type, $money);

        if (false === $res) {
            $this->error(500, '参数错误');
        }
        if (200 != $res['code']) {
            $this->error($res['code'], $res['info']);
        }
        $this->success('打赏成功');
    }

    /**
     * 打赏列表接口
     * type comment/video
     * id   comment_id/video_id
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function lists()
    {
        $id = I('id');
        $type = I('type', '', 'trim');

        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $res = event('reward')->lists($type, $id, $p, $limit);

        if (false === $res) {
            $this->error(500, '参数错误');
        }
        $this->success('请求成功', $res);
    }

    /**
     * 打赏选项+余额
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function options()
    {
        $this->checkLogin();
        $user = event('reward')->userinfo(UID);

        $options_str = C('REWARD_OPTIONS');

        $options = array_map('intval', explode(',', $options_str));
        
        if (false === $options || false === $user) {
            $this->error(500, '参数错误');
        }

        $this->success('请求成功', ['user' => $user, 'options' => $options]);
    }

    /**
     * 个人信息接口(余额)
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function userinfo() {
        $this->checkLogin();
        $user = event('reward')->userinfo(UID);
        if (false === $user) {
            $this->error(500, '参数错误');
        }
        $this->success('请求成功', $user);
    }

    /**
     * 打赏/被打赏记录
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function record()
    {
        $uid = I('uid', 0, 'intval');
        if ($uid) {
            $isRecive = true;
        } else {
            $this->checkLogin();
            $uid = UID;
            $type = I('type', 'recive', 'trim');
            $isRecive = $type == 'recive';
        }

        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $res = event('reward')->record($uid, $isRecive, $p, $limit);

        if (false === $res) {
            $this->error(500, '参数错误');
        }
        $this->success('请求成功', $res);
    }

}